package daoImp;

import Util.FileIOUtil;
import dao.UserDao;
import entity.User;

import java.util.ArrayList;

public class UserDaoImp implements UserDao {
    FileIOUtil fio = new FileIOUtil();
    String usersFile = "users";
    String top = "tops";
    @Override
    public boolean login(String name, String pwd) {
        User m = getUser(name);
        if (m==null)
            return false;
        if (m.login(pwd))
            return true;
        return false;
    }

    @Override
    public ArrayList<User> getUsers() {
        ArrayList<User> al;
        al = fio.input(usersFile);
        return al;
    }

    @Override
    public User getUser(int id) {
        ArrayList<User> al = getUsers();
        if (al.size()==0)
            return null;
        for (User m:al
        ) {
            if (m.getuId()==id){
                return m;
            }
        }
        return null;
    }

    @Override
    public User getUser(String name) {
        ArrayList<User> al = getUsers();
        if (al.size()==0)
            return null;
        for (User m:al
        ) {
            if (m.getName().equals(name)){
                return m;
            }
        }
        return null;
    }

    @Override
    public boolean addUser(User user) {
        ArrayList<User> al = getUsers();
        boolean isOk = al.add(user);
        boolean isOk2 = fio.output(usersFile,al);
        return isOk&&isOk2;
    }

    @Override
    public boolean editUser(User user) {
        if (getUser(user.getuId())==null)
            return false;
        ArrayList<User> al = getUsers();
        for (int i = 0; i < al.size(); i++) {
            if (al.get(i).getuId()==user.getuId()){
                al.get(i).updata(user);
                return fio.output(usersFile,al);
            }
        }
        return false;
    }

    @Override
    public ArrayList<User> getTopUps() {
        ArrayList<User> al;
        al = fio.input(top);
        return al;
    }

    @Override
    public boolean topUp(User user) {
        ArrayList<User> al = getTopUps();
        boolean isOk = al.add(user);
        boolean isOk2 = fio.output(top,al);
        return isOk&&isOk2;
    }

    @Override
    public boolean deleteTop(User user) {
        ArrayList<User> al = getTopUps();
        if (al.size()==0)
            return false;
        if (getTop(user.getName())!=null){
            al.remove(user);
            fio.output(top,al);
        }

        return false;
    }

    @Override
    public User getTop(String name) {
        ArrayList<User> al = getTopUps();
        if (al.size()==0)
            return null;
        for (User m:al
        ) {
            if (m.getName().equals(name)){
                return m;
            }
        }
        return null;
    }

    @Override
    public int getId() {
        ArrayList<User> al = getUsers();
        int x = al.size()+1;
        boolean flag = false;
        for (int i = 1; i <= x; i++) {
            flag = false;
            for (User e:al
            ) {
                if (e.getuId()==i){
                    flag = true;
                }
            }
            if (!flag){
                x = i;
                break;
            }
        }
        return x;
    }


}
